package thread;

/**
 *
 */
public class SyncDemo2 {
    public static void main(String[] args) {
        Shop shop1=new Shop();
        Shop shop2=new Shop();
        Thread t1=new Thread("王解放"){
            public void run(){
                shop1.buy();
            }
        };
        Thread t2=new Thread("李顶级"){
            public void run(){
                shop2.buy();
            }
        };
        Thread t3=new Thread("许流年"){
            public void run(){
                shop2.buy();
            }
        };
        t1.start();
        t2.start();
        t3.start();

    }

}
class Shop{
    public  void buy(){

        try {Thread t=Thread.currentThread();
            System.out.println(t+"正在挑衣服");
            Thread.sleep(5000);
            //同步监视器对象通常还有临界资源（抢谁锁谁）
//            synchronized (new Object){
//            synchronized ("hello"){
            synchronized (this){
                System.out.println(t+"正在试衣服");
                Thread.sleep(5000);
            }

            System.out.println(t+"离开");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
